﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DmxLib
{
    public class Rs485DMX1024Send
    {

        private int frame;
        private byte[] sendData;
        private System.Timers.Timer timer;
        private System.IO.Ports.SerialPort port;
        /// <summary>
        /// DMX 发送类  按帧率发循环发送
        /// </summary>
        /// <param name="frame">帧率 默认30</param>
        public Rs485DMX1024Send(string comName, int frame = 30)
        {

            sendData = new byte[1024];
            this.frame = frame;
            port = new System.IO.Ports.SerialPort(comName);
            port.StopBits = System.IO.Ports.StopBits.Two;


        }
        private byte[] set = new byte[1];
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (port.IsOpen)
                {
                    port.BaudRate = 100000;
                    port.Write(set, 0, 1);

                    port.BaudRate = 250000;
                    //var buf = new byte[1 + universeData.Length];
                    //universeData.CopyTo(buf, 1);
                    port.Write(sendData, 0, sendData.Length);
                }
            }
            catch (Exception ex)
            {

            }




        }
        /// <summary>
        /// 更新缓存的数据
        /// </summary>
        /// <param name="dm512">长度为0-1023 值为0-255的数据包</param>
        public void UpdateData(byte[] dmxval)
        {
            //if (dm512 == null || universe>UniverseCount||universe<0)
            //    return;
            //for (int i = 0; i < dm512.Length&&i<512; i++)
            //{
            //    sendData[universe][i] = dm512[i];
            //}
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            for (int i = 0; i < dmxval.Length && i < 1024; i++)
            {
                sendData[i] = dmxval[i];
            }
        }
        /// <summary>
        /// 按索引更新  索引0-1023
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="value">值</param>
        public void UpdateData(int index, byte value)
        {

            if (index >= 0 & index < 1024)
                sendData[index] = value;
        }
        /// <summary>
        /// 按起始位置更新数据 超出不更新
        /// </summary>
        /// <param name="startIndex"> 缓存数组的起始位置</param>
        /// <param name="value">数据索引</param>
        public void UpdateData(int startIndex, byte[] dmxval)
        {
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            if (startIndex >= 0 & startIndex < 1024)
            {
                for (int i = startIndex; i < dmxval.Length - startIndex && i < 512; i++)
                {
                    sendData[i] = dmxval[i];
                }
            }
        }
        /// <summary>
        /// 按起始位置结束更新数据 超出不更新 更新目标数组的起始结束位置
        /// </summary>
        /// <param name="value">需要更新的数组</param>
        /// <param name="startIndex">缓存数组的起始位置</param>
        /// <param name="endIndex">需要更新原数组的长度</param>
        public void UpdateData(byte[] dmxval, int startIndex, int count)
        {
            if (dmxval == null)
                throw new ArgumentNullException("dmxval is null");
            if (dmxval.Length < count)
                throw new ArgumentException("dmxval 数组长度比count小");
            if (startIndex >= 0 & startIndex < 1024)
            {
                for (int i = startIndex, j = 0; i < dmxval.Length - startIndex && i < 1024 && j < count; i++, j++)
                {
                    sendData[i] = dmxval[i];
                }
            }
        }
        private bool isStart = false;

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 是否启动
        /// </summary>
        public bool IsStart
        {
            private set
            {
                isStart = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsStart"));
            }
            get => isStart;
        }
        /// <summary>
        /// 启动 按帧率发送数据
        /// </summary>
        public void Start()
        {
            if (IsStart)
                return;
            //Stop();
            port.Open();
            timer = new System.Timers.Timer();
            timer.Elapsed += Timer_Elapsed;
            timer.Interval = 1000 / frame;
            timer.Start();
            IsStart = true;
        }
        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            if (!IsStart)
                return;
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
                timer = null;

            }
            port.Close();
            IsStart = false;
        }
        ~Rs485DMX1024Send()
        {

            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
                timer = null;

            }
        }
        public void Dispose()
        {
            Stop();
            port.Dispose();
        }
    }
}
